package ytdp.careagle.com.ytdp.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.entity.PushCarEntity;
import ytdp.careagle.com.ytdp.entity.QueryAllCarType;
import ytdp.careagle.com.ytdp.entity.SellCarProgramEntity;
import ytdp.careagle.com.ytdp.entity.ValuationQuery;
import ytdp.careagle.com.ytdp.mvp.mode.CarEvaluateModel;
import ytdp.careagle.com.ytdp.mvp.view.IEvaluateResultView;
import ytdp.careagle.com.ytdp.net.MyObserver;
import ytdp.careagle.com.ytdp.net.NetWorks;
import ytdp.careagle.com.ytdp.net.Result;
import ytdp.careagle.com.ytdp.net.Results;
import ytdp.careagle.com.ytdp.utils.DateUtils;

/**
 * Created by admin on 2017/9/26.
 */

public class EvaluateResultPresenter {
    private IEvaluateResultView view;
    private CarEvaluateModel model;
    private int year;
    private ValuationQuery data;

    public EvaluateResultPresenter(IEvaluateResultView view) {
        this.view = view;
        model = new CarEvaluateModel();
    }

    public void getNextYeayPrice(ValuationQuery data) {
        this.data = data;
        String purchasePredictPrice = data.getPurchasePredictPrice();
        try {
            Double nextYearPurchasePrice = jsonPaser(new JSONObject(purchasePredictPrice), "purchase");
            view.setNextYearPrice(nextYearPurchasePrice);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private double jsonPaser(JSONObject jsonObject, String purchase) {
        double value = 0;
        Iterator iterator = jsonObject.keys();                       // joData是JSONObject对象
        while (iterator.hasNext()) {
            String key = iterator.next() + "";
            try {
                value = (Double) jsonObject.get(key);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

    public void getfindByModelCode(Context context) {
        HashMap<String, String> map = new HashMap<>();
//        map.put("modeCode",);
        model.findByModelCode(context, G.getHeader(context), map, new MyObserver<Result>() {
            @Override
            public void onNext(Result result) {
                super.onNext(result);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    public void getValuationData(Context context, final int state) {
        Intent intent = view.getDataIntent();
        String modelCode = intent.getStringExtra("modelCode");
        String mileage = intent.getStringExtra("mileage");
        String cityCode = intent.getStringExtra("cityCode");
        String firstRegDate = intent.getStringExtra("firstRegDate");
        String modelName = intent.getStringExtra("modelName");
        String carSeriesCode = intent.getStringExtra("carSeriesCode");
        String carSeriesName = intent.getStringExtra("carSeriesName");
        String brandCode = intent.getStringExtra("brandCode");
        String brandName = intent.getStringExtra("brandName");
        String provinceCode = intent.getStringExtra("provinceCode");
        String provinceName = intent.getStringExtra("provinceName");
        String cityName = intent.getStringExtra("cityName");
        String imageUrl = intent.getStringExtra("imageUrl");
        HashMap<String, String> map = new HashMap<>();
        map.put("modelCode", modelCode);
        map.put("mileage", mileage);
        map.put("cityCode", cityCode);
        map.put("firstRegDate", firstRegDate);
        map.put("modelName", modelName);
        map.put("carSeriesCode", carSeriesCode);
        map.put("carSeriesName", carSeriesName);
        map.put("brandCode", brandCode);
        map.put("brandName", brandName);
        map.put("provinceCode", provinceCode);
        map.put("provinceName", provinceName);
        map.put("cityName", cityName);
        String rankCode = "2";
        if (state == 0) {
            rankCode = "3";
        } else if (state == 1) {
            rankCode = "2";
        } else if (state == 2) {
            rankCode = "1";
        }
        map.put("rankCode", rankCode);
        if (!TextUtils.isEmpty(imageUrl)) {
            map.put("imageUrl", imageUrl);
        } else {
            map.put("imageUrl", "");
        }
        view.showProgress("正在请求数据");
        model.queryValuationQuery(context, G.getHeader(context), map, new MyObserver<Result<ValuationQuery>>() {
            @Override
            public void onNext(Result<ValuationQuery> valuationQueryResult) {
                super.onNext(valuationQueryResult);
                if (valuationQueryResult.getCode() == G.REQUEST_OK) {
                    view.setValuationData(valuationQueryResult.getData(), state);
                } else {
                    view.showMessage(valuationQueryResult.getMessage());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showMessage("数据请求失败");
                view.hideProgress();
            }
        });
    }

    public void findVehicleSeriesByResidualvalue(Context context) {
        HashMap<String, String> map = new HashMap<>();
        String price = view.getPrice();
        map.put("price", price);
        model.findVehicleSeriesByResidualvalue(context, G.getHeader(context), map, new MyObserver<Result<PushCarEntity>>() {
            @Override
            public void onNext(Result<PushCarEntity> result) {
                super.onNext(result);
                if (result.getCode() == G.REQUEST_OK) {
                    view.setTips(result.getData().getTips());
                    view.setPushCarData(result.getData().getVehicleSeriesList());
                } else {
                    view.showMessage(result.getMessage());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showMessage("服务器错误");
            }
        });
    }

    public void findVehicleByVehicleSeriesAndOnline(Context context, String id) {
        view.showProgress("正在加载");
        model.findVehicleByVehicleSeriesAndOnline(context, G.getHeader(context), id, new MyObserver<Results<SellCarProgramEntity>>() {
            @Override
            public void onNext(Results<SellCarProgramEntity> result) {
                super.onNext(result);
                if (result.getCode() == G.REQUEST_OK) {
                    view.setCarData(result.getData());
                } else {
                    view.showMessage(result.getMessage());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showMessage("服务器错误");
                view.hideProgress();
            }
        });
    }


    public void queryAllCar(Context context) {
        model.queryAllCar(context, G.getHeader(context), new MyObserver<Results<QueryAllCarType>>() {
            @Override
            public void onNext(Results<QueryAllCarType> result) {
                super.onNext(result);
                if (result.getCode() == G.REQUEST_OK) {
                    view.setAllCar(result.getData());
                } else {
                    view.showMessage(result.getMessage());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showMessage("服务器错误");
            }
        });
    }
}
